package com.java.receiver;

import com.java.instant.MqConst;
import com.java.model.hosp.Schedule;
import com.java.model.vo.MsmVo;
import com.java.model.vo.OrderMqVo;
import com.java.service.RabbitService;
import com.java.service.ScheduleService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

public class ScheduleReceiver {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private RabbitService rabbitService;

    //更新排班的监听器 QueueBinding 队列绑定 value --- key --- router
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER,durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER),
            key = {MqConst.ROUTING_ORDER}
    ))
    public void recevier(OrderMqVo orderMqVo, Message message, Channel channel){

        if(orderMqVo.getAvailableNumber()!=null){

        Schedule schedule = scheduleService.getById(orderMqVo.getScheduleId());
        //可预约数
        schedule.setReservedNumber(orderMqVo.getReservedNumber());
        //剩余预约数
        schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
        //更新排班信息
        scheduleService.updateSchedule(schedule);
        }else{
            Schedule schedule = scheduleService.getById(orderMqVo.getScheduleId());
            int availableNumber = schedule.getAvailableNumber().intValue() + 1;
            schedule.setAvailableNumber(availableNumber);
            scheduleService.updateSchedule(schedule);

        }
        MsmVo msmVo = orderMqVo.getMsmVo();
        //通过rabbiteUtil来进行发送触发监听器 发送数据
        if(msmVo != null ){
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM,MqConst.ROUTING_MSM_ITEM,msmVo);
        }
    }
}
